// Fill out your copyright notice in the Description page of Project Settings.


#include "Core/Character2D.h"
#include "PaperFlipbookComponent.h"
#include "GameFramework/CharacterMovementComponent.h"


void ACharacter2D::BeginPlay()
{
	Super::BeginPlay();
}


void ACharacter2D::FlipSprite(float Horizontal)
{
	if (FMath::IsNearlyZero(Horizontal, 0.01f))
		return;

	float TargetDirection = FMath::Sign(Horizontal);
	if (!FMath::IsNearlyEqual(TargetDirection, CurrentFacingDirection, 0.01f))
	{
		CurrentFacingDirection = TargetDirection;
		if (UPaperFlipbookComponent* Flipbook = GetSprite())
		{
			FVector Scale = Flipbook->GetRelativeScale3D();
			Scale.X = FMath::Abs(Scale.X) * Horizontal;
			Flipbook->SetRelativeScale3D(Scale);
		}
	}
}

void ACharacter2D::PlayAnimation(const FName& AnimName)
{
	if (!AnimationMap.Contains(AnimName))
	{
		UE_LOG(LogTemp, Error, TEXT("动画不存在：%s"), *AnimName.ToString())
		return;
	}

	if (UPaperFlipbookComponent* Flipbook = GetSprite())
	{
		Flipbook->SetFlipbook(AnimationMap[AnimName]);
	}
}

void ACharacter2D::Movement(const FVector& Direction)
{
	AddMovementInput(Direction);
}

void ACharacter2D::DashStart()
{
	GetCharacterMovement()->SetMovementMode(MOVE_Flying);
	FVector DashDirection = FVector(0, CurrentFacingDirection, 0);
	UE_LOG(LogTemp, Log, TEXT("冲刺方向：%s"), *DashDirection.ToString())
	// 应用闪避力
	LaunchCharacter(DashDirection * DashSpeed, true, true);
}

void ACharacter2D::DashEnd()
{
	GetCharacterMovement()->SetMovementMode(MOVE_Walking);
}
